package br.com.ebc.icontatos.dao;

import java.util.List;

import br.com.ebc.icontatos.genericdao.GenericDAO;
import br.com.ebc.icontatos.model.LoginUsuario;
import br.com.ebc.icontatos.repository.LoginRepository;

public class LoginDAO extends GenericDAO<LoginUsuario, Integer> implements LoginRepository{

	@Override
	public LoginUsuario findLogin(String email, String senha) {
		LoginUsuario login = execute(email, senha);
		return login;
	}

	@Override
	public boolean existLogin(String email, String senha) {
		List<LoginUsuario> login = executeSQL(email, senha);
		return (login.size() > 0 ) ? true : false;
	}

	@Override
	public LoginUsuario execute(String email, String senha) {
		return (LoginUsuario) getEntityManager().createQuery("select l from LoginUsuario as l where l.email = '" + email + "' and l.senha = '" + senha + "' and l.ativo = '1'").getSingleResult();
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<LoginUsuario> executeSQL(String email, String senha) {
		return (List<LoginUsuario>) getEntityManager().createQuery("select l from LoginUsuario as l where l.email = '" + email + "' and l.senha = '" + senha + "' and l.ativo = '1'").getResultList();
	}

	@SuppressWarnings("unchecked")
	@Override
	public boolean existEmail(String email) {
		List<LoginUsuario> obj = (List<LoginUsuario>) getEntityManager().createQuery("select l from LoginUsuario as l where l.email = '" + email + "'").getResultList();
		if(obj.size() > 0){
			return true;
		} else {
			return false;
		}
	}

	@Override
	public LoginUsuario findLogin(String email) {
		return (LoginUsuario) getEntityManager().createQuery("select l from LoginUsuario as l where l.email = '" + email + "'").getSingleResult();
	}

}
